import type {
  SearchBaseSelectors,
  SearchPageSelectors,
} from '../../../../module/page_enhancer/appearance/style_enhancement/platforms'
import { AppStyleCollections } from '../../../../types/constants'

/**
 * 页面类型
 */
export enum SogouPageType {
  Ask = 'ask',
  Encyclopedia = 'encyclopedia',
  Knowledge = 'knowledge',
  Medical = 'medical',
  News = 'news',
  Video = 'video',
  Web = 'web',
  Weixin = 'weixin',
  Zhihu = 'zhihu',
  More = 'more',
  IndexSearch = 'index-search',
  IndexWeixin = 'index-weixin',
  IndexKnowledge = 'index-knowledge',
  IndexEncyclopedia = 'index-encyclopedia',
  NotFound = 'not-found',
  WeixinNotFound = 'weixin-not-found',
  Undefined = 'undefined',
  // 暂不支持
  IndexVideo = 'index-video',
  App = 'app',
  Chinese = 'chinese',
  Translation = 'translation',
}

/**
 * 页面类型属性
 */
export interface SogouPageTypeResult {
  type: SogouPageType | 'default'
}

// ====================================================== 基础配置 ======================================================
// 搜索框容器
export const SOGOU_SEARCH_BOX_SELECTOR: string = '#sogou_wrap_id #searchForm'
// 搜索结果项
export const SOGOU_SEARCH_RESULT_CONTAINER_SELECTOR: string = '#sogou_wrap_id #wrapper #main .results'
// 广告结果项
export const SOGOU_SEARCH_AD_RESULT_CONTAINER_SELECTOR: string = '#sogou_wrap_id #wrapper #main .sponsored-ams'
// 基础选择器
export const SOGOU_SEARCH_BASE_SELECTORS: SearchBaseSelectors = {
  searchBox: {
    input: '#searchForm input#upquery',
    dropdown: {
      wrapper: SOGOU_SEARCH_BOX_SELECTOR,
      menu: ``,
      hotRank: `${SOGOU_SEARCH_BOX_SELECTOR} .sbox-hotrank-container`,
      suggestions: `${SOGOU_SEARCH_BOX_SELECTOR} .sbox-sugg-container`,
      history: `${SOGOU_SEARCH_BOX_SELECTOR} .sbox-history-container`,
      clearHistoryBtn: `${SOGOU_SEARCH_BOX_SELECTOR} .sbox-history-container .sbox-history-clear`,
      clearHistoryText: '清空历史',
    },
  },
  result: {
    container: SOGOU_SEARCH_RESULT_CONTAINER_SELECTOR,
    item: `${SOGOU_SEARCH_RESULT_CONTAINER_SELECTOR} > .vrwrap`,
  },
  adResult: {
    container: SOGOU_SEARCH_AD_RESULT_CONTAINER_SELECTOR,
    item: `${SOGOU_SEARCH_AD_RESULT_CONTAINER_SELECTOR} > .ad_result`,
    itemTitle: `${SOGOU_SEARCH_AD_RESULT_CONTAINER_SELECTOR} > .ad_result h4`,
  },
  rightAside: {
    containerSelector: '#sogou_wrap_id #wrapper #right',
    dragTargetSelector: `.${AppStyleCollections.Drag.AREA}`,
    dragTargetStyle: `bottom: -18px; left: -32px;`,
    handleIconStyle: ``,
    position: 'left',
  },
}

// 页面选择器
export const SOGOU_SEARCH_PAGE_SELECTORS: SearchPageSelectors = {
  [SogouPageType.Web]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}, ${SOGOU_SEARCH_BASE_SELECTORS.adResult.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult, .rb), ${SOGOU_SEARCH_BASE_SELECTORS.adResult.item}`,
    },
    rightAside: {
      ...SOGOU_SEARCH_BASE_SELECTORS.rightAside,
      dragTargetSelector: `.atGradientLine`,
    },
  },
  [SogouPageType.Weixin]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    searchBox: {
      input: '.header-box .header [name=searchForm] input#query',
      dropdown: {
        wrapper: ``,
        menu: ``,
        hotRank: ``,
        suggestions: `.header-box .header [name=searchForm] .suggestion .suglist > li:not([history])`,
        history: `.header-box .header [name=searchForm] .suggestion .suglist > li[history]`,
        clearHistoryBtn: '> .close',
        clearHistoryText: '删除',
      },
    },
    result: {
      container: '#wrapper #main .news-list',
      item: '#wrapper #main .news-list > li',
    },
  },
  [SogouPageType.Zhihu]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult)`,
    },
  },
  [SogouPageType.Medical]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult)`,
    },
  },
  [SogouPageType.Video]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    searchBox: {
      input: '#header .[name=searchForm] input#query',
      dropdown: {
        wrapper: ``,
        menu: ``,
        hotRank: `#header [name=searchForm] .suggestion:has(.s_title)`,
        suggestions: `#header [name=searchForm] .suggestion:not(:has(.s_title))`,
        history: ``,
        clearHistoryBtn: '',
        clearHistoryText: '',
      },
    },
    result: {
      container: '#wrap #container  .short-video-list ul',
      item: '#wrap #container .short-video-list ul > .short-video-item',
    },
  },
  [SogouPageType.Ask]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}, ${SOGOU_SEARCH_BASE_SELECTORS.adResult.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult, .rb), ${SOGOU_SEARCH_BASE_SELECTORS.adResult.item}`,
    },
  },
  [SogouPageType.Encyclopedia]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}, ${SOGOU_SEARCH_BASE_SELECTORS.adResult.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult, .rb), ${SOGOU_SEARCH_BASE_SELECTORS.adResult.item}`,
    },
  },
  [SogouPageType.News]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: SOGOU_SEARCH_BASE_SELECTORS.result.container,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.container} > :is(.vrwrap, .reactResult)`,
    },
  },
  [SogouPageType.Knowledge]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    result: {
      container: `${SOGOU_SEARCH_BASE_SELECTORS.result.container}, ${SOGOU_SEARCH_BASE_SELECTORS.adResult.container}`,
      item: `${SOGOU_SEARCH_BASE_SELECTORS.result.item}, ${SOGOU_SEARCH_BASE_SELECTORS.adResult.item}`,
    },
  },
  [SogouPageType.IndexSearch]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    searchBox: {
      input: '#content #search-box #sf input#query',
      dropdown: {
        wrapper: ``,
        menu: ``,
        hotRank: ``,
        suggestions: `#content #search-box .sbox-sugg-container`,
        history: `#content #search-box .sbox-history-container`,
        clearHistoryBtn: '#content #search-box .sbox-history-container .sbox-history-clear',
        clearHistoryText: '清空历史',
      },
    },
  },
  [SogouPageType.IndexWeixin]: {
    ...SOGOU_SEARCH_BASE_SELECTORS,
    searchBox: {
      input: '.content #searchForm .search-in-box input#query',
      dropdown: {
        wrapper: ``,
        menu: ``,
        hotRank: ``,
        suggestions: `.content #searchForm .search-in-box .suggestion .suglist > li:not([history])`,
        history: `.content #searchForm .search-in-box .suggestion .suglist > li[history]`,
        clearHistoryBtn: `.content #searchForm .search-in-box .suggestion .suglist > li[history] > .close`,
        clearHistoryText: '删除',
      },
    },
  },
}
// 常用选择器
export const SOGOU_SEARCH_SELECTORS: Record<'indexSearch', Record<'input' | 'suggestionContainer' | 'suggestionItem', string>> = {
  indexSearch: {
    input: SOGOU_SEARCH_PAGE_SELECTORS[SogouPageType.IndexSearch]!.searchBox.input,
    suggestionContainer: SOGOU_SEARCH_PAGE_SELECTORS[SogouPageType.IndexSearch]!.searchBox.dropdown.suggestions,
    suggestionItem: `${SOGOU_SEARCH_PAGE_SELECTORS[SogouPageType.IndexSearch]!.searchBox.dropdown.suggestions} .sbox-sugg-wrap .sbox-sugg-item`,
  },
}
